/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prototype.vaadin;

import com.prototype.console.Connection;
import com.prototype.console.JobThread;
import com.vaadin.Application;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.*;
import java.io.File;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mdcolin
 */
public class Index extends Application {
    /**.
     * @param appPath
     */
    private String appPath;
    /**.
     * define the windows size
     * @param WINDOWSIZE
     */
    private static final int WINDOWSIZE = 200;

    @Override
    public void init() {
        final Window mainWindow = new Window("Recover SF Code");
        try {
            appPath = "C:\\Users\\mdcolin\\Documents\\NetBeansProjects\\"
                    + "trunk\\trunk";
            /**
             * Recovering properties file
             */
            AppProperties prop = new AppProperties(appPath + File.separator
                    + "recover_sf_code2" + File.separator + "web"
                    + File.separator + "WEB-INF" + File.separator
                    + "french_label.properties");

            /**
             * Create the subwindow "About us"
             */
            final Window subwindow = new Window(
                    prop.getPropValue("item_about_us"));
            subwindow.setCaption(prop.getPropValue("item_about_us"));
            subwindow.setModal(true);
            subwindow.setWidth(WINDOWSIZE, Sizeable.UNITS_PIXELS);
            subwindow.setResizable(false);

            /**
             * Define a command for "About us" item
             */
            // menubar cmdaboutus
            // Create a Menubar
            final MenuBar menubar = new MenuBar();
            menubar.setSizeFull();
            // menubar
            // Subwindows data
            Label lblAuthors = new Label("<center>"
                    + "This application was developed by : <br/>"
                    + "BOUKHEZAR Zineddine<br/>"
                    + "COLIN MD<br/>"
                    + "MAIGA Fatoumata<br/>"
                    + "MOUNISSAMY Nadia</center>");
            lblAuthors.setSizeFull();

            lblAuthors.setContentMode(Label.CONTENT_XHTML);
            // Page title
            Label lblTitle = new Label("<h1><center>"
                    + "Recover SourceForge Code</center></h1>");
            final int value1 = 50;
            lblTitle.setHeight(value1, Sizeable.UNITS_PIXELS);
            lblTitle.setContentMode(Label.CONTENT_XHTML);
            subwindow.addComponent(lblAuthors);
            mainWindow.addComponent(menubar);
            mainWindow.addComponent(lblTitle);

            tableSheet(mainWindow, prop);

        } catch (Exception ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        }
        setMainWindow(mainWindow);
    }

     /**.
      *
      * @param tableResult
      * @return exppath
      */
    public TextField poolThread(Table tableResult) {
        Connection test;
        TextField exppath = new TextField("Chemin :",
                "http://sourceforge.net/directory/os:windows"
                + "/license:publicdomain/");
        exppath.setWidth("500px");
        test = new Connection(exppath.toString());
        int numWorkers = test.nbPage();
        numWorkers--;
        ExecutorService pooldynamique = Executors.newCachedThreadPool();
        JobThread[] workers = new JobThread[numWorkers];
        Future<ConcurrentHashMap<String, String>>[] futures =
                new Future[numWorkers];
        for (int i = 0; i < numWorkers; i++) {
            workers[i] = new JobThread(test.urlPage().get(i));
            futures[i] = pooldynamique.submit(workers[i]);
        }
        pooldynamique.shutdown();
        for (int i = 0; i < numWorkers; i++) {
            try {
                test.getConcurrentMap().putAll((futures[i].get()));
            } catch (Exception ex) {
                Logger.getLogger(Index.class.getName())
                        .log(Level.SEVERE, null, ex);
            }
        }
        int i = 0;
        for (ConcurrentHashMap.Entry<String, String> entry
                :test.getConcurrentMap().entrySet()) {
            String cle = entry.getKey();
            String valeur = entry.getValue();
            int value = i;
            tableResult.addItem(
                        new Object[]{cle, valeur, i}, value);
            i++;
        }

        return exppath;
    }
    /**.
     * @param mainWindow
     * @param prop
     * @throws Exception
     */
    public void tableSheet(Window mainWindow, AppProperties prop) {
        TabSheet tabsMain;
        // Tab 1 content
        VerticalLayout tabHome = new VerticalLayout();
        tabHome.setMargin(true);
        Resource dd = new ClassResource("image/sourceforge.png", this);
        Embedded embddSfPicture = new Embedded("SourceForge Logo", dd);
        tabHome.addComponent(embddSfPicture);
        // Tab 2 content
        VerticalLayout tabSearch = new VerticalLayout();
        tabSearch.setMargin(true);
        SearchInterface searchInterface = new SearchInterface();
        searchInterface.setAppPath(appPath);
        try {
            searchInterface.recoverForm(tabSearch);
        } catch (Exception ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        }
        // Tab 3 content
        VerticalLayout tabResult = new VerticalLayout();
        tabResult.setMargin(true);
        // Create the table with a caption
        Table tableResult = new Table(prop.getPropValue(
                "table_result_caption"));
        tableResult.setSizeFull();

        // Define the names and data types of columns
        // The "default value" parameter is meaningless here
        tableResult.addContainerProperty(
                prop.getPropValue("table_result_col1"), String.class,  null);
        tableResult.addContainerProperty(
                prop.getPropValue("table_result_col2"), String.class,  null);
        tableResult.addContainerProperty(
                prop.getPropValue("table_result_col3"), String.class, null);


        // code  de newmain

        TextField exppath = poolThread(tableResult);
        tabResult.addComponent(exppath);
        tabResult.addComponent(tableResult);

        // Add the tab in the tabSheet
        tabsMain = new TabSheet();
        tabsMain.setSizeFull();

        tabsMain.addTab(tabHome, prop.getPropValue("item_home"), null);
        tabsMain.addTab(tabSearch, prop.getPropValue("subwindow_search"),
                null);
        tabsMain.addTab(tabResult, prop.getPropValue("subwindow_result"),
                null);

        mainWindow.addComponent(tabsMain);
    }
}
